package com.gobo.statisticsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gobo.commonutils.R;
import com.gobo.statisticsservice.clients.UcenterMemberClient;
import com.gobo.statisticsservice.pojo.StatisticsDaily;
import com.gobo.statisticsservice.mapper.StatisticsDailyMapper;
import com.gobo.statisticsservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.statisticsservice.utils.DateUtil;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterMemberClient ucenterMemberClient;


    @Override
    public Boolean saveStatisticsByDay(String day) {
        R r = ucenterMemberClient.registerCount(day);
        System.out.println(r.getData().get("countRegister"));
        Integer countRegister = (Integer) r.getData().get("countRegister");
        System.out.println(countRegister);
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateCalculated(day);
        //TODO
        statisticsDaily.setCourseNum(RandomUtils.nextInt(0, 200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(0, 200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(0, 200));
        //判断是否已经统计   已经统计就删除后再加入最新数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            baseMapper.delete(queryWrapper);
        }
        int insert = baseMapper.insert(statisticsDaily);
        return insert == 1;
    }

    @Override
    public Map<String, Object> getChartData(String typeName, String begin, String end) {

        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        //如果均为空  直接返回最近一个月的数据
        if (StringUtils.isEmpty(begin) && StringUtils.isEmpty(end)){
            end = DateUtil.formatDate(new Date());
            begin = DateUtil.formatDate(DateUtil.addDays(new Date(),-30));
        }
        queryWrapper.between("date_calculated", begin, end);
        List<StatisticsDaily> statisticsDailyList = baseMapper.selectList(queryWrapper);
        //数据集合
        Map<String, Object> dataMap = new HashMap<>();
        //日期集合
        List<Integer> loginNumList = new ArrayList<>();
        List<Integer> videoViewNumList = new ArrayList<>();
        List<Integer> courseNumList = new ArrayList<>();
        List<Integer> registerNumList = new ArrayList<>();
        List<HashMap<String, Object>> xAxis = new ArrayList<>();
        List<HashMap<String, Object>> yAxis = new ArrayList<>();
        List<HashMap<String, Object>> series = new ArrayList<>();
        List<String> legendList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < statisticsDailyList.size(); i++) {
            StatisticsDaily daily = statisticsDailyList.get(i);
            dateList.add(daily.getDateCalculated());
            loginNumList.add(daily.getLoginNum());
            videoViewNumList.add(daily.getVideoViewNum());
            courseNumList.add(daily.getCourseNum());
            registerNumList.add(daily.getRegisterNum());
        }
        HashMap<String, Object> xmap = new HashMap<>();
        xmap.put("type", "category");
        xmap.put("data", dateList);
        xmap.put("name","日期");
        HashMap<String, Object> axisPointer = new HashMap<>();
        axisPointer.put("type", "shadow");
        xmap.put("axisPointer", axisPointer);
        xAxis.add(xmap);
        HashMap<String, Object> ymap1 = new HashMap<>();
        ymap1.put("type", "value");
        ymap1.put("name", "人数统计");
        yAxis.add(ymap1);
        if (StringUtils.isEmpty(typeName)){
            typeName = "all";
        }
        if (typeName.equals("login_num")) {
            HashMap<String, Object> series1 = new HashMap<>();
            series1.put("type", "bar");
            series1.put("name", "登录人数");
            series1.put("data", loginNumList);
            series.add(series1);

            legendList.add("登录人数");

        } else if (typeName.equals("register_num")) {

            HashMap<String, Object> series2 = new HashMap<>();
            series2.put("type", "bar");
            series2.put("name", "注册人数");
            series2.put("data", registerNumList);
            series.add(series2);

            legendList.add("注册人数");

        } else if (typeName.equals("video_view_num")) {

            HashMap<String, Object> ymap3 = new HashMap<>();
            ymap3.put("type", "line");
            ymap3.put("name", "播放视频数");
            ymap3.put("data", videoViewNumList);
            series.add(ymap3);
            legendList.add("播放视频数");
        } else if (typeName.equals("course_num")) {
            HashMap<String, Object> ymap4 = new HashMap<>();
            ymap4.put("type", "line");
            ymap4.put("name", "新增课程数");
            ymap4.put("data", courseNumList);
            series.add(ymap4);
            legendList.add("新增课程数");
        }else {
                HashMap<String, Object> series1 = new HashMap<>();
                series1.put("type", "bar");
                series1.put("name", "登录人数");
                series1.put("data", loginNumList);
                series.add(series1);
                HashMap<String, Object> series2 = new HashMap<>();
                series2.put("type", "bar");
                series2.put("name", "注册人数");
                series2.put("data", registerNumList);
                series.add(series2);
                HashMap<String, Object> ymap3 = new HashMap<>();
                ymap3.put("type", "line");
                ymap3.put("name", "播放视频数");
                ymap3.put("data", videoViewNumList);
                series.add(ymap3);
                HashMap<String, Object> ymap4 = new HashMap<>();
                ymap4.put("type", "line");
                ymap4.put("name", "新增课程数");
                ymap4.put("data", courseNumList);
                series.add(ymap4);
                legendList.add("登录人数");
                legendList.add("注册人数");
                legendList.add("播放视频数");
                legendList.add("新增课程数");
        }

        dataMap.put("legend", legendList);
        dataMap.put("xAxis", xAxis);
        dataMap.put("yAxis", yAxis);
        dataMap.put("series", series);
        map.put("dataMap", dataMap);
        return map;
    }


}
